package lib.api;

import lib.XML.XMLHelper;
import lib.api.cache.CacheManager;
import lib.api.cache.DiskCacheManager;
import lib.logging.Logger;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by IntelliJ IDEA. User: lodder Date: 20/08/11 Time: 15:11 To change
 * this template use File | Settings | File Templates.
 */
public class XmlHTTPAPI {

	private CacheManager ucm;
	private DiskCacheManager dcm;
	private String userAgent;
	private int ratelimit;
	private int dayRatelimit;
	
	public XmlHTTPAPI() {
		ucm = CacheManager.getURLCache();
		dcm = DiskCacheManager.getDiskCache();
	}

	public Document getXML(String url) {
		return getXML(url, 900);
	}

	public Document getXML(String url, long timeout) {
		Logger.instance.trace("XmlHTTPAPI", "getXML", "timeout: " + timeout);
		try {
			if (userAgent != null) {
				ucm.setUserAgent(userAgent);
			}
			ucm.setRatelimit(ratelimit);
			ucm.setDayRateLimit(dayRatelimit);
			return XMLHelper.getDocument(ucm.fetchAsInputStream(new URL(url),
					timeout));
		} catch (MalformedURLException e) {
			Logger.instance.error(Logger.stack2String(e));
		} catch (ParserConfigurationException e) {
			Logger.instance.error(Logger.stack2String(e));
		} catch (SAXException e) {
			Logger.instance.error(Logger.stack2String(e));
		} catch (IOException e) {
			Logger.instance.error(Logger.stack2String(e));
		} catch (Exception e) {
			Logger.instance.error(Logger.stack2String(e));
		}
		return null;
	}

	public Document getXMLDisk(String url) {
		// cache timout is 30 dagen
		return getXMLDisk(url, 24 * 60 * 60 * 30);
	}

	public Document getXMLDisk(String url, long timeout) {
		Logger.instance.trace("XmlHTTPAPI", "getXMLDisk", "timeout: " + timeout);
		try {
			if (userAgent != null) {
				dcm.setUserAgent(userAgent);
			}
			dcm.setRatelimit(ratelimit);
			dcm.setDayRateLimit(dayRatelimit);
			return XMLHelper.getDocument(dcm.fetchAsInputStream(new URL(url),
					timeout));
		} catch (MalformedURLException e) {
			Logger.instance.error(Logger.stack2String(e));
		} catch (ParserConfigurationException e) {
			Logger.instance.error(Logger.stack2String(e));
		} catch (SAXException e) {
			Logger.instance.error(Logger.stack2String(e));
		} catch (IOException e) {
			Logger.instance.error(Logger.stack2String(e));
		} catch (Exception e) {
			Logger.instance.error(Logger.stack2String(e));
		}
		return null;
	}

	public void removeCacheEntry(String url) {
		dcm.removeEntry(url);
		ucm.removeEntry(url);
	}

	/**
	 * @return the userAgent
	 */
	public String getUserAgent() {
		return userAgent;
	}

	/**
	 * @param userAgent
	 *            the userAgent to set
	 */
	public void setUserAgent(String userAgent) {
		this.userAgent = userAgent;
	}

	/**
	 * @return the ratelimit
	 */
	public int getRatelimit() {
		return ratelimit;
	}

	/**
	 * @param ratelimit
	 *            the ratelimit to set
	 */
	public void setRatelimit(int ratelimit) {
		this.ratelimit = ratelimit;
	}

	/**
	 * @return the dayRatelimit
	 */
	public int getDayRatelimit() {
		return dayRatelimit;
	}

	/**
	 * @param dayRatelimit the dayRatelimit to set
	 */
	public void setDayRatelimit(int dayRatelimit) {
		this.dayRatelimit = dayRatelimit;
	}
}
